import { createElement } from "./createElement"
import { updateChildren } from "./updateChildren"

export function patchVnode(oldVNode, newVNode) {
    if (newVNode.text === undefined) {
        if (oldVNode.children.length > 0 && newVNode.children.length > 0) {
            // 新老节点都有 children
            updateChildren(oldVNode.elm, oldVNode.children, newVNode.children)
        }else if(newVNode.children.length > 0){
            // 只有新节点有 children
            oldVNode.elm.innerText = ''
            newVNode.children.forEach(child=>{
                child.elm = createElement(child)
                oldVNode.elm.appendChild(child.elm)
            })
        }
    } else if (newVNode.text !== oldVNode.text) {
        // 新节点只有 text ，并且和老节点不一致，没有 children
        oldVNode.elm.innerText = newVNode.text
    }
}